//yoduao 7;youdao 8;
var ws;

function getSocket(){
	ws = new WebSocket('ws://localhost:3000/youdao');
	ws.onopen = function() {
		console.log("open");
	}
	ws.onmessage = function(e) {
		wurd_socket_translate(e.data);
	}
	ws.onclose = function(e) {
		ws.close();
		ws = null;
	}
	ws.onerror = function(e) {
		ws.close();
		ws = null;
	}
}
getSocket();
setInterval(function(){
    if(!ws || ws.readyState != 1){
		try{
			ws && ws.close();
		}catch(error){}
		try{
			getSocket();
		}catch(error){}
    }
},3000)

//setTimeout(function(){
//	var from = "zh-CHS";
//	var to = "en";
//	var lang = from == "zh-CHS"?to:from;
//	var dd = {keyword:"我的",lang:lang,type:"quick"};//quick;detail;ldetail
//	ydk.getDictResult({data:dd,complete:null,fail:null,success:function(e){
//		doLog(e);
//	}})
//},5000);

function wurd_socket_translate(keyword) {
    keyword = keyword ? keyword : "you are my love";
    var dd = {
        "keyword": keyword,
        "from": "en",
        "to": "zh-CHS",
        "keyfrom": "deskdict.main",
        "forTranslatePage": true,
        "type": "net"//local;net(will be faster)
    };
    var ddd = {
        data: dd,
        complete: null,
        fail: function(e) {
            ws.send(JSON.stringify({ code: 500, msg: e }));
        },
        success: function(e) {
            ws.send(JSON.stringify({ code: 200, msg: e }));
        }
    };
    ydk.getTranslateResult(ddd)
}

function wurd_socket_word_translate(keyword) {
	var from = "zh-CHS";
	var to = "en";
	var lang = from == "zh-CHS"?to:from;
	var dd = {keyword:keyword,lang:lang,type:"quick"};//quick;detail;ldetail
	ydk.getDictResult({
		data:dd,
		complete:null,
		fail:function(e){
			ws.send(JSON.stringify({ code: 500, msg: e }));
		},success:function(e){
			ws.send(JSON.stringify({ code: 200, msg: e }));
		}
	});
}